/**
 * @author ahammer
 * @date 2009
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Look;
import main.Manager;
import main.Tank;


public class IqmInvertGUI extends IqmOperatorJFrame implements  IqmOperatorGUIBuilder,
															    ActionListener {

	private  ParameterBlockJAI      pb           = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame     opGUIFrame   = null;
	//private  IqmInvertGUI           opGUIFrame   = null;  //actionPerformed events werden in dieser Klasse abgehandelt

	private  ButtonGroup            group        = null;
	private  JRadioButtonMenuItem   buttInvLut   = null;
	private  JRadioButtonMenuItem   buttInvImg   = null;
	private  JToolBar               jToolBarOpt  = null;
	

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (buttInvImg.isSelected()) pb.setParameter("invertOption", 0);
		if (buttInvLut.isSelected()) pb.setParameter("invertOption", 1);
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current values to the parameter block
	 *
	 */
	@Override
	public void setParameterValues(){
		buttInvImg.setSelected(false);
		buttInvLut.setSelected(false);
		if (pb.getIntParameter(0) == 0 )  buttInvImg.setSelected(true);
		if (pb.getIntParameter(0) == 1 )  buttInvLut.setSelected(true);		
	}
	/**
	 * This is the default constructor
	 */
	public IqmInvertGUI() {
		super();
		//initialize();
		//this.createAndShowGUI();;
	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//this = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//this = new IqmInvertGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("Invert");  //wie pb nur ohne iqm
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height);
		this.getJContentPane().add(getJToolBarOpt(), getGridBagConstraintsButtonGroupOptInv());
		this.pack();
		if (pb == null) pb = new ParameterBlockJAI("iqminvert");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqminvert"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		setParameterValues();
	}

	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
		
	}
	
	/**
	 * This method initializes the Option Invert LUT 	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtInvLut() {
		if (buttInvLut == null) {
			buttInvLut = new JRadioButtonMenuItem();
			buttInvLut.setText("Invert LUT");
			buttInvLut.addActionListener(this);
			buttInvLut.setActionCommand("parameter");
		}
		return buttInvLut;
	}
	/**
	 * This method initializes the Option Invert Image 	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtInvImg() {
		if (buttInvImg == null) {
			buttInvImg = new JRadioButtonMenuItem();
			buttInvImg.setText("Invert Image");
			buttInvImg.addActionListener(this);
			buttInvImg.setActionCommand("parameter");
		}
		return buttInvImg;
	}
	
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarOpt() {
//		if (jToolBarOpt == null) {
			jToolBarOpt = new JToolBar("Options");
			jToolBarOpt.setOrientation(1); //Vertical orientation
			jToolBarOpt.add(getJRadioButtonMenuButtInvImg());
			jToolBarOpt.add(getJRadioButtonMenuButtInvLut());			
			jToolBarOpt.addSeparator();
			this.setButtonGroupOptInv();	//Gruppieren der JRadioButtons
//		}
		return jToolBarOpt;
	}
	private  void setButtonGroupOptInv() {
	//if (ButtonGroup group == null) {
		group = new ButtonGroup();
		//group.add(getJRadioButtonMenuButtInvLut());
		//group.add(getJRadioButtonMenuButtInvImg());
		group.add(buttInvImg);
		group.add(buttInvLut);
		
	}
	private  GridBagConstraints getGridBagConstraintsButtonGroupOptInv(){
		GridBagConstraints gridBagConstraintsButtonGroupOptInv = new GridBagConstraints();
		gridBagConstraintsButtonGroupOptInv.gridx = 1;
		gridBagConstraintsButtonGroupOptInv.gridy = 4;
		gridBagConstraintsButtonGroupOptInv.gridwidth = 3;
		gridBagConstraintsButtonGroupOptInv.insets = new Insets(0,0,20,0);
		//gridBagConstraintsButtonGroupOptInv.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonGroupOptInv;
	}	

	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmInvert".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmInvertGUI gui = new IqmInvertGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
